package com.cn.kehong.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.IEnergyService;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/energy")
public class EnergyController {

    @Autowired
    private IEnergyService iEnergyService;

    //能源按照厂房初始化列表页面（能耗统计）
    @RequestMapping("/getEnergyByWorkshop/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getEnergyByWorkshop(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  energyList = iEnergyService.getEnergyByWorkshop(queryForm);
        response.setResult(energyList);
        response.setSuccess(true);
        return response;
    }
    //按照能源初始化列表页面（能耗统计）
    @RequestMapping("/getEnergyByType/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getEnergyByType(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  energyList = iEnergyService.getEnergyByType(queryForm);
        response.setResult(energyList);
        response.setSuccess(true);
        return response;
    }

    //按照能源初始化列表页面（能耗趋势）
    @RequestMapping("/getEnergyByTrend/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getEnergyByTrend(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  energyList = iEnergyService.getEnergyByTrend(queryForm);
        response.setResult(energyList);
        response.setSuccess(true);
        return response;
    }



    //按照能源初始化列表页面（能耗趋势）
    @RequestMapping("/getEnergyData/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getEnergyData(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  energyList = iEnergyService.getEnergyData(queryForm);
        response.setResult(energyList);
        response.setSuccess(true);
        return response;
    }




    //能源下载接口
    @ResponseBody
    @RequestMapping("/exportEnergy")
    public void exportEnergy(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setStartTime(jsonObject.getString("startTime"));
            queryForm.setEndTime(jsonObject.getString("endTime"));
            queryForm.setWorkshop(jsonObject.getString("workshop"));
            queryForm.setEnergyType(jsonObject.getString("energyType"));
            queryForm.setQueryTime(jsonObject.getString("queryTime"));
            queryForm.setQueryName(jsonObject.getString("queryName"));
        }
        iEnergyService.exportEnergy(queryForm,response);
    }



    //初始化能耗统计列表页面(新)电
    @RequestMapping("/getListByElectricity/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getListByElectricity(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  energyList = iEnergyService.getListByElectricity(queryForm);
        response.setResult(energyList);
        response.setSuccess(true);
        return response;
    }


    //初始化能耗统计列表页面(新)水
    @RequestMapping("/getListByWater/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Energy>> getListByWater(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<Energy>> response = new JsonResponse<>();
        List<Energy> Energys = iEnergyService.getListByWater(queryForm);
        response.setResult(Energys);
        response.setSuccess(true);
        return response;
    }

    //初始化能耗统计列表页面(新)气
    @RequestMapping("/getListByGas/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Energy>> getListByGas(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<Energy>> response = new JsonResponse<>();
        List<Energy> Energys = iEnergyService.getListByGas(queryForm);
        response.setResult(Energys);
        response.setSuccess(true);
        return response;
    }

    //初始化能耗统计列表页面(新)光伏
    @RequestMapping("/getListByLight/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Energy>> getListByLight(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<Energy>> response = new JsonResponse<>();
        List<Energy> Energys = iEnergyService.getListByLight(queryForm);
        response.setResult(Energys);
        response.setSuccess(true);
        return response;
    }

    //园区下拉框
    @RequestMapping("/getWorkShopList/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getWorkShopList(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  energyList = iEnergyService.getWorkShopList(queryForm);
        response.setResult(energyList);
        response.setSuccess(true);
        return response;
    }



    //数据查询
    @RequestMapping("/getDataList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Energy>> getDataList(@PathVariable(value = "frontToken", required = true) String frontToken, QueryForm queryForm) throws Exception {
        JsonResponse<List<Energy>> response = new JsonResponse<>();
        List<Energy> Energys = iEnergyService.getDataList(queryForm);
        response.setResult(Energys);
        response.setSuccess(true);
        return response;
    }


    //看板数据接口
    @RequestMapping("/getDataListByKb")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getDataListByKb( QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  energyList = iEnergyService.getDataListByKb(queryForm);
        response.setResult(energyList);
        response.setSuccess(true);
        return response;
    }
    //初始化能耗统计列表页面(新)电看板
    @RequestMapping("/getListByElectricity")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getListByElectricitykb(QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object>> response = new JsonResponse<Map<String,Object>>();
        Map<String,Object>  energyList = iEnergyService.getListByElectricity(queryForm);
        response.setResult(energyList);
        response.setSuccess(true);
        return response;
    }


    //初始化能耗统计列表页面(新)水看板
    @RequestMapping("/getListByWater")
    @ResponseBody
    public JsonResponse<List<Energy>> getListByWater(QueryForm queryForm) throws Exception {
        JsonResponse<List<Energy>> response = new JsonResponse<>();
        List<Energy> Energys = iEnergyService.getListByWater(queryForm);
        response.setResult(Energys);
        response.setSuccess(true);
        return response;
    }

    //初始化能耗统计列表页面(新)气看板
    @RequestMapping("/getListByGas")
    @ResponseBody
    public JsonResponse<List<Energy>> getListByGas( QueryForm queryForm) throws Exception {
        JsonResponse<List<Energy>> response = new JsonResponse<>();
        List<Energy> Energys = iEnergyService.getListByGas(queryForm);
        response.setResult(Energys);
        response.setSuccess(true);
        return response;
    }

}
